{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Definition for a binary tree node.\n",
    "# class TreeNode(object):\n",
    "#     def __init__(self, x):\n",
    "#         self.val = x\n",
    "#         self.left = None\n",
    "#         self.right = None\n",
    "\n",
    "class Solution(object):\n",
    "    max = 0\n",
    "    def diameterOfBinaryTree(self, root):\n",
    "        self.findMax(root)\n",
    "        return self.max\n",
    "    def findMax(self,node):\n",
    "        if(node == None):\n",
    "            return 0\n",
    "        else:\n",
    "            l = self.findMax(node.left)\n",
    "            r = self.findMax(node.right)\n",
    "            if(l+r>self.max):\n",
    "                self.max = l+r\n",
    "            return max(l,r)+1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
